<?php
/**
 *  获取订单列表信息
 *
 * @access  public
 * @param
 * @return void
 */

function Export_order_list($type='',$arr = '')
{
	$result = get_filter();
	if ($result === false)
	{
		if($arr != '' && !empty($arr) && $arr['search_flag'] == 1){
			$filter['order_sn'] = $arr['order_sn'];
			$filter['consignee'] = $arr['consignee'];
			$filter['composite_status'] = $arr['composite_status'];
		}else if($arr != '' && !empty($arr) && $type == 'cw_order_list'){
			$filter['order_sn'] = $arr['order_sn'];
		}else{
			/* 过滤信息 */
			$filter['order_sn'] = empty($_REQUEST['order_sn']) ? '' : trim($_REQUEST['order_sn']);
			$filter['consignee'] = empty($_REQUEST['consignee']) ? '' : trim($_REQUEST['consignee']);
			$filter['composite_status'] = isset($_REQUEST['composite_status']) ? intval($_REQUEST['composite_status']) : -1;
		}
		if (!empty($_GET['is_ajax']) && $_GET['is_ajax'] == 1)
		{
			$_REQUEST['consignee'] = json_str_iconv($_REQUEST['consignee']);
			//$_REQUEST['address'] = json_str_iconv($_REQUEST['address']);
		}
		$filter['email'] = empty($_REQUEST['email']) ? '' : trim($_REQUEST['email']); //邮箱
		$filter['address'] = empty($_REQUEST['address']) ? '' : trim($_REQUEST['address']);//地址
		$filter['zipcode'] = empty($_REQUEST['zipcode']) ? '' : trim($_REQUEST['zipcode']);//邮编
		$filter['tel'] = empty($_REQUEST['tel']) ? '' : trim($_REQUEST['tel']);//电话
		$filter['mobile'] = empty($_REQUEST['mobile']) ? 0 : intval($_REQUEST['mobile']);//手机
		$filter['country'] = empty($_REQUEST['country']) ? 0 : intval($_REQUEST['country']);//国家
		$filter['province'] = empty($_REQUEST['province']) ? 0 : intval($_REQUEST['province']);//省分
		$filter['city'] = empty($_REQUEST['city']) ? 0 : intval($_REQUEST['city']);//城市
		$filter['district'] = empty($_REQUEST['district']) ? 0 : intval($_REQUEST['district']);//地区
		$filter['shipping_id'] = empty($_REQUEST['shipping_id']) ? 0 : intval($_REQUEST['shipping_id']);//配送方式
		$filter['pay_id'] = empty($_REQUEST['pay_id']) ? 0 : intval($_REQUEST['pay_id']);//支付方式
		$filter['order_status'] = isset($_REQUEST['order_status']) ? intval($_REQUEST['order_status']) : -1;//订单状态
		$filter['shipping_status'] = isset($_REQUEST['shipping_status']) ? intval($_REQUEST['shipping_status']) : -1;//配送状态
		$filter['pay_status'] = isset($_REQUEST['pay_status']) ? intval($_REQUEST['pay_status']) : -1;//支付状态
		$filter['user_id'] = empty($_REQUEST['user_id']) ? 0 : intval($_REQUEST['user_id']);//用户ID
		$filter['user_name'] = empty($_REQUEST['user_name']) ? '' : trim($_REQUEST['user_name']);//用户名
		$filter['group_buy_id'] = isset($_REQUEST['group_buy_id']) ? intval($_REQUEST['group_buy_id']) : 0;//团购ID
		$filter['sort_by'] = empty($_REQUEST['sort_by']) ? 'add_time' : trim($_REQUEST['sort_by']);
		$filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
		if($type == 'kf_order_list'){
			$filter['sort_by'] = 'pay_status';
			$filter['sort_order'] = 'asc';
		}

		$filter['start_time'] = empty($_REQUEST['start_time']) ? '' : (strpos($_REQUEST['start_time'], '-') > 0 ?  local_strtotime($_REQUEST['start_time']) : $_REQUEST['start_time']);
		$filter['end_time'] = empty($_REQUEST['end_time']) ? '' : (strpos($_REQUEST['end_time'], '-') > 0 ?  local_strtotime($_REQUEST['end_time']) : $_REQUEST['end_time']);
		$filter['delivery_station_id'] = empty($_REQUEST['delivery_station_id']) ? 0 : $_REQUEST['delivery_station_id'];
		$where = 'WHERE 1 ';
		if ($filter['order_sn'])
		{
			$where .= " AND o.order_sn LIKE '%" . mysql_like_quote($filter['order_sn']) . "%'";
		}
		if ($filter['consignee'])
		{
			$where .= " AND o.consignee LIKE '%" . mysql_like_quote($filter['consignee']) . "%'";
		}
		if ($filter['email'])
		{
			$where .= " AND o.email LIKE '%" . mysql_like_quote($filter['email']) . "%'";
		}
		if ($filter['address'])
		{
			$where .= " AND o.address LIKE '%" . mysql_like_quote($filter['address']) . "%'";
		}
		if ($filter['zipcode'])
		{
			$where .= " AND o.zipcode LIKE '%" . mysql_like_quote($filter['zipcode']) . "%'";
		}
		if ($filter['tel'])
		{
			$where .= " AND o.tel LIKE '%" . mysql_like_quote($filter['tel']) . "%'";
		}
		if ($filter['mobile'])
		{
			$where .= " AND o.mobile LIKE '%" .mysql_like_quote($filter['mobile']) . "%'";
		}
		if ($filter['country'])
		{
			$where .= " AND o.country = '$filter[country]'";
		}
		if ($filter['province'])
		{
			$where .= " AND o.province = '$filter[province]'";
		}
		if ($filter['city'])
		{
			$where .= " AND o.city = '$filter[city]'";
		}
		if ($filter['district'])
		{
			$where .= " AND o.district = '$filter[district]'";
		}
		if ($filter['shipping_id'])
		{
			$where .= " AND o.shipping_id  = '$filter[shipping_id]'";
		}
		if ($filter['pay_id'])
		{
			$where .= " AND o.pay_id  = '$filter[pay_id]'";
		}
		if ($filter['order_status'] != -1)
		{
			$where .= " AND o.order_status  = '$filter[order_status]'";
		}
		if ($filter['shipping_status'] != -1)
		{
			$where .= " AND o.shipping_status = '$filter[shipping_status]'";
		}
		if ($filter['pay_status'] != -1)
		{
			$where .= " AND o.pay_status = '$filter[pay_status]'";
		}
		if ($filter['user_id'])
		{
			$where .= " AND o.user_id = '$filter[user_id]'";
		}
		if ($filter['user_name'])
		{
			$where .= " AND u.user_name LIKE '%" . mysql_like_quote($filter['user_name']) . "%'";
		}
		if ($filter['start_time'])
		{
			$where .= " AND o.add_time >= '$filter[start_time]'";
		}
		if ($filter['end_time'])
		{
			$where .= " AND o.add_time <= '$filter[end_time]'";
		}
		if ($filter['delivery_station_id'] != -1)
		{
			$where .= " AND o.delivery_station_id = ".$filter[delivery_station_id];
		}

		$sql = 'select pay_id from '.$GLOBALS['os']->table('payment')." WHERE is_cod = 1 ";
		$is_code = $GLOBALS['db']->getOne($sql);


		if( 'shipping_list' == $type ){

			$where .= " AND (o.shipping_status = ".SS_UNSHIPPED." AND ((o.pay_status = ".PS_UNPAYED." AND o.order_status = ".OS_CONFIRMED." AND o.pay_id = ".$is_code.") OR ((o.pay_status = ".PS_PAYED." AND o.order_status != ".OS_CANCELED." AND o.order_status != ".OS_INVALID." ))))";

			if($arr != '' && !empty($arr) && $arr['search_flag'] == 2){
				if($arr['shipping_info'] != -1 ){
					$where .= " AND o.shipping_id = ".$arr['shipping_info'];
				}

				if($arr['time_start'] != '' && $arr['time_end'] != ''){
					$time_start = intval(strtotime($arr['time_start'])) - 8*60*60;
					$time_end   = intval(strtotime($arr['time_end'])) - 8*60*60;
					$where .= " AND add_time > ".$time_start." AND add_time < ".$time_end;
				}
			}
		}else if('cw_order_list' == $type){
			if($arr['cw_status'] == 'to_pay'){
				$where .= " AND ((o.order_status = ".OS_CONFIRMED." OR o.order_status = ".OS_UNCONFIRMED.") AND o.pay_status = ".PS_UNPAYED.")";
			}elseif ($arr['cw_status'] == 'to_finish'){
				$where .= " AND o.order_status = ".OS_CONFIRMED." AND o.pay_status = ".PS_PAYED." AND o.shipping_status = ".SS_SHIPPED;
			}else{
				$where .= " AND ((o.order_status = ".OS_CONFIRMED." OR o.order_status = ".OS_UNCONFIRMED.") AND (o.pay_status = ".PS_UNPAYED." OR (o.order_status = ".OS_CONFIRMED." AND o.pay_status = ".PS_PAYED." AND o.shipping_status = ".SS_SHIPPED.")))";
			}
		}else if('kf_order_list' == $type){
			$where .= " AND ( o.order_status = ".OS_UNCONFIRMED." AND ( o.lock_status = 0 OR o.lock_status = ".$_SESSION[admin_id]."))";
		}

		if(isset($filter['composite_status'])){
			//综合状态
			switch($filter['composite_status'])
			{
				case CS_AWAIT_PAY :
					$where .= order_query_sql('await_pay');
					break;

				case CS_AWAIT_SHIP :
					$where .= order_query_sql('await_ship');
					break;

				case CS_FINISHED :
					$where .= order_query_sql('finished');
					break;

				case PS_PAYING :
					if ($filter['composite_status'] != -1)
					{
						$where .= " AND o.pay_status = '$filter[composite_status]' ";
					}
					break;

				default:
					if ($filter['composite_status'] != -1)
					{
						$where .= " AND o.order_status = '$filter[composite_status]' ";
					}
			}
		}
		/* 团购订单 */
		if ($filter['group_buy_id'])
		{
			$where .= " AND o.extension_code = 'group_buy' AND o.extension_id = '$filter[group_buy_id]' ";
		}

		/* 如果管理员属于某个办事处，只列出这个办事处管辖的订单 */
		$sql = "SELECT agency_id FROM " . $GLOBALS['os']->table('admin_user') . " WHERE user_id = '$_SESSION[admin_id]'";
		$agency_id = $GLOBALS['db']->getOne($sql);
		if ($agency_id > 0)
		{
			$where .= " AND o.agency_id = '$agency_id' ";
		}


		/* 记录总数 */
		if ($filter['user_name'])
		{
			$sql = "SELECT COUNT(*) FROM " . $GLOBALS['os']->table('order_info') . " AS o ,".
			$GLOBALS['os']->table('users') . " AS u " . $where;
		}
		else
		{
			$sql = "SELECT COUNT(*) FROM " . $GLOBALS['os']->table('order_info') . " AS o ". $where;
		}
        		/* 分页大小 */
		$filter['page'] = empty($_REQUEST['page']) || (intval($_REQUEST['page']) <= 0) ? 1 : intval($_REQUEST['page']);
        $filter['page_size'] = $GLOBALS['db']->getOne($sql);
		/* 查询 */
		$sql = "SELECT o.order_id, o.zipcode,o.invoice_no,o.order_sn, o.add_time, o.order_status, o.shipping_status, o.order_amount, o.money_paid, o.pay_id," .
		"o.pay_status, o.lock_status, o.consignee, o.address, o.email, o.tel, o.extension_code, o.extension_id, o.shipping_name,o.invoice_status, " .
		"(" . order_amount_field('o.') . ") AS total_fee, " .
		"IFNULL(u.user_name, '" .'匿名客户'. "') AS buyer,".
        "open_order_goods.order_id,open_order_goods.goods_sn,open_order_goods.goods_name".
		" FROM " . $GLOBALS['os']->table('order_info') . " AS o " .
		//" LEFT JOIN " .$GLOBALS['os']->table('users'). " AS u ON u.user_id=o.user_id ". $where .
        " LEFT JOIN " .$GLOBALS['os']->table('users'). " AS u ON u.user_id=o.user_id ".
        " Inner Join open_order_goods ON open_order_goods.order_id = o.order_id " . $where .
		" ORDER BY $filter[sort_by] $filter[sort_order] ".
		" LIMIT " . ($filter['page'] - 1) * $filter['page_size'] . ",$filter[page_size]";
		foreach (array('order_sn', 'consignee', 'email', 'address', 'zipcode', 'tel', 'user_name') AS $val)
		{
			$filter[$val] = stripslashes($filter[$val]);
		}
		set_filter($filter, $sql);
	}
	else
	{
		$sql    = $result['sql'];
		$filter = $result['filter'];
	}

	$row = $GLOBALS['db']->getAll($sql);
	/* 格式话数据 */
	foreach ($row AS $key => $value)
	{
	    /*增加地区查询 by 杨敬君*/
        $sql = "SELECT concat(IFNULL(c.region_name, ''), '  ', IFNULL(p.region_name, ''), " .
		"'  ', IFNULL(t.region_name, ''), '  ', IFNULL(d.region_name, '')) AS region " .
		"FROM " . $GLOBALS['os']->table('order_info') . " AS o " .
		"LEFT JOIN " . $GLOBALS['os']->table('region') . " AS c ON o.country = c.region_id " .
		"LEFT JOIN " . $GLOBALS['os']->table('region') . " AS p ON o.province = p.region_id " .
		"LEFT JOIN " . $GLOBALS['os']->table('region') . " AS t ON o.city = t.region_id " .
		"LEFT JOIN " . $GLOBALS['os']->table('region') . " AS d ON o.district = d.region_id " .
		"WHERE o.order_id = '$value[order_id]'";
        $row[$key]['region']= $GLOBALS['db']->getOne($sql);        
		$row[$key]['formated_order_amount'] = price_format($value['order_amount']);
		$row[$key]['formated_money_paid'] = price_format($value['money_paid']);
		$row[$key]['formated_total_fee'] = price_format($value['total_fee']);
		$row[$key]['short_order_time'] = local_date('Y-m-d H:i', $value['add_time']);
		if ($value['order_status'] == OS_INVALID || $value['order_status'] == OS_CANCELED)
		{
			/* 如果该订单为无效或取消则显示删除链接 */
			$row[$key]['can_remove'] = 1;
		}
		else
		{
			$row[$key]['can_remove'] = 0;
		}

		$where = "where op.log_info like '%".$value['order_sn']."'";
		$where_print = $where." and op.print_type = 1";
		$where_delivery_print .= $where." and op.print_type = 3";

		$sql = 'SELECT COUNT(*) FROM ' .$GLOBALS['os']->table('order_printlog'). ' AS op ' . $where_print;
		$count = $GLOBALS['db']->getOne($sql);
		$order_print_flag = $count >0 ?1:0;

		$sql = 'SELECT COUNT(*) FROM ' .$GLOBALS['os']->table('order_printlog'). ' AS op ' . $where_delivery_print;
		$count = $GLOBALS['db']->getOne($sql);
		$order_delivery_print_flag = $count >0 ?1:0;

		$row[$key]['print_flag'] = $order_print_flag;
		$row[$key]['delivery_print_flag'] = $order_delivery_print_flag;
		$where = '';
		$where_print = '';
		$where_delivery_print = '';
	}



	$arr = array('orders' => $row, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);

	return $arr;
}




?>